package org.ikongfuzi;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput;
import com.sun.syndication.io.XmlReader;

@SuppressWarnings("serial")
public class IkongfuziServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		doPost(req,resp);
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
//		resp.setContentType("text/plain");
//		resp.getWriter().println("Hello, world");
		String url = req.getParameter("rssurl");
		try {
			//URL feedUrl = new URL("http://rss.sina.com.cn/news/marquee/ddt.xml");
			URL feedUrl = new URL(url);
			SyndFeedInput input = new SyndFeedInput();
			SyndFeed feed = input.build(new XmlReader(feedUrl));
			//Writer writer = new FileWriter("./test.xml");   
			//System.out.println("type:"+feed.getFeedType());
			//feed.setFeedType("rss_2.0"); // 
			SyndFeedOutput output = new SyndFeedOutput();   
			List ls = feed.getEntries();
			Iterator ir = ls.iterator();
			SyndEntry ets = null;
			SyndContent desc = null;
			//System.out.println("channel:"+feed.getTitle());
			StringBuffer buf = new StringBuffer("");
			while(ir.hasNext()){
				ets = (SyndEntry)ir.next();
				desc =  ets.getDescription();
				buf.append("<h4>").append(ets.getTitle()).append("<a target='_blank' href='").append(ets.getLink()).append("' >").append("@查看原文").append("</a>").append("</h4>");
				buf.append("<p>").append(desc.getValue()).append("</p>");
				buf.append("<HR  size=1 noshade='noshade'/>");
				//buf.append("<a target='_blank' href='").append(ets.getLink()).append("' >").append(ets.getLink()).append("</a>");
//				System.out.println("item-title:"+ets.getTitle());
//				System.out.println("desc:"+desc.getValue());
//				System.out.println("link:"+ets.getLink());
			}
			
			resp.setContentType("text/html");
			resp.setCharacterEncoding("GBK");
			ServletOutputStream os = resp.getOutputStream();
			os.write(("<h3 id='rssTitle' name='rssTitle' >"+feed.getTitle()+"</h3>").getBytes("GB2312"));
			os.write(buf.toString().getBytes("GBK"));
			os.close();
			//System.out.print(output.outputString(feed));
//			writer.close(); 
//			Map map = new HashMap();
//			map.put("url", url);
//			map.put("rssName", url);
//			ReaderRSS rRSS = new ReaderRSS();
//			rRSS.insertRssinfo(map);
			//List lis = rRSS.getRssinfoByName("");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FeedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		}
	}
}
